define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'orderon', title: __('Orderon'), operate: 'LIKE'},
                        {field: 'goods', title: "商品", formatter:function(value,row,index){
							var goods=""
							for(var i=0;i<row.goods.length;i++){
								for(var j=0;j<row.goods.length;j++){
									if(row.goods[i].id==row.json[j].id){
										var num=row.json[j].num;
									}
								}
								goods+=row.goods[i].name+'*'+num;
							}
							return goods;
						}},
						{field: 'price', title: __('Price'), operate:'BETWEEN'},
						{field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'kno', title: __('Kno'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"8":__('Status 8')}, formatter: Table.api.formatter.status},
						{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function(value,row,index){
                            if(row.status == 1){
                                return '<button id="btn" data-id="'+ row.id +'">发货</button>'
                            }else{
                                return '--'
                            }
                        }}
                    ]
                ],
				queryParams: function(params){
					
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    filter.type="goods";
					params.filter=JSON.stringify(filter);

                    
                    return params;
                }
            });
            var index
            $(document).on('click','#btn',function(){
                var dom =   `<div style="padding:20px">
                                快递单号：<input type="text" class="orderInp">
                                <button class="orderbtn" data-id="${$(this).attr('data-id')}">确定</button>
                            </div>`
                index = layer.open({
                    title:'订单信息',
                    type:1,
                    content:dom
                    })
            })
            $(document).on('click','.orderbtn',function(){
                $.ajax({
                    type:'POST',
                    data:{
                        id:$(this).attr('data-id'),
                        kno:$('.orderInp').val()
                    },
                    url:'order/order_fa',
                    success:function(res){
                        if(res.code == 1){
                            layer.msg(res.msg)
                            layer.close(index); 
                            $(".btn-refresh").trigger("click");
                        }else{
                            layer.msg(res.msg)
                        }
                    }
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
		shop: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/shop' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'orderon', title: __('Orderon'), operate: 'LIKE'},
                        {field: 'goods', title: "商品", formatter:function(value,row,index){
							var goods=""
							for(var i=0;i<row.goods.length;i++){
								for(var j=0;j<row.goods.length;j++){
									if(row.goods[i].id==row.json[j].id){
										var num=row.json[j].num;
									}
								}
								goods+=row.goods[i].name+'*'+num;
							}
							return goods;
						}},
						{field: 'price', title: __('Price'), operate:'BETWEEN'},
						{field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
 
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('未核销'),"2":__('Status 2'),"8":__('已核销')}, formatter: Table.api.formatter.status},
						{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                       
                    ]
                ],
				queryParams: function(params){
					
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    filter.type="ticket";
					filter.shop_id= Config.ids;
					params.filter=JSON.stringify(filter);

                    
                    return params;
                }
            });
            
            $(document).on('click','.orderbtn',function(){
                $.ajax({
                    type:'POST',
                    data:{
                        id:$(this).attr('data-id'),
                        kno:$('.orderInp').val()
                    },
                    url:'order/order_fa',
                    success:function(res){
                        if(res.code == 1){
                            layer.msg(res.msg)
                            layer.close(index); 
                            $(".btn-refresh").trigger("click");
                        }else{
                            layer.msg(res.msg)
                        }
                    }
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
		
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});